# 导入数据分析包：numpy（科学计算）、pandas（处理数据框）和 matplotlib/seaborn(可视化)
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import csv
import warnings
import WorldCupsSummary
import wordcloud
warnings.filterwarnings("ignore")

# 导入数据：

matches = pd.read_csv('WorldCupMatches.csv')
print(matches)

# 中国队参加的比赛
China = matches[(matches['Away Team Name'] == 'China PR') | (matches['Home Team Name'] == 'China PR')]
print(China)

# 统一“联邦德国”和“德国”
matches = matches.replace(['Germany FR'], 'Germany')

# 类型转化
matches['Home Team Goals'] = matches['Home Team Goals'].astype(int)
matches['Away Team Goals'] = matches['Away Team Goals'].astype(int)

# 格式化比赛结果，如 3-2
matches['result'] = matches['Home Team Goals'].astype(str) + "-" + matches['Away Team Goals'].astype(str)
print(matches)
#########################################################################################################################
top5_attendance = matches.sort_values(by='Attendance', ascending=False)[:5]
print(top5_attendance)

top5_attendance['VS'] = top5_attendance['Home Team Name'] + " VS " + top5_attendance['Away Team Name']

# top5_attendance['attend']= top5_attendance['Attendance'].astype(str)

plt.figure(figsize=(12, 10))

ax = sns.barplot(y=top5_attendance['VS'], x=top5_attendance['Attendance'])
sns.despine(right=True)

plt.ylabel('Teams')
plt.xlabel('Attendence Number')
plt.title('Top5 Hottest Match')

for i, s in enumerate(
        "Stadium: " + top5_attendance['Stadium'] + "\nDate: " + top5_attendance['Datetime'] + "\nAttendance: " +
        top5_attendance['Attendance'].astype(str)):
    ax.text(2000, i, s, fontsize=12, color='white')

########################################################################################################################
matches['total_goals'] = matches['Home Team Goals'] + matches['Away Team Goals']
matches['VS'] = matches['Home Team Name'] + " VS " + matches['Away Team Name']

top10_goals = matches.sort_values(by='total_goals', ascending=False)[:10]

top10_goals['VS'] = top10_goals['Home Team Name'] + " VS " + top10_goals['Away Team Name']

top10_goals['total_goals_str'] = top10_goals['total_goals'].astype(str) + " goals scored"

top10_goals['Home Team Goals'] = top10_goals['Home Team Goals'].astype(int)
top10_goals['Away Team Goals'] = top10_goals['Away Team Goals'].astype(int)

top10_goals['result'] = top10_goals['Home Team Goals'].astype(str) + "-" + top10_goals['Away Team Goals'].astype(str)

plt.figure(figsize=(12, 10))
ax = sns.barplot(y=top10_goals['VS'], x=top10_goals['total_goals'])
sns.despine(right=True)
plt.ylabel('Match')
plt.xlabel('Goals')
plt.yticks(size=10)
plt.xticks(size=10)
plt.title('Top10 Goals Match', size=20)

for i, s in enumerate("Stadium " + top10_goals['Stadium'] + ", Date: " + top10_goals['Datetime'] + "\n" +
                      top10_goals['total_goals_str'] + ", match result: " + top10_goals['result']):
    ax.text(1, i, s, fontsize=12, color='white', va='center')

################################################################################################################################
# 取主客场进球数的绝对值
matches['difference_goals'] = pd.Series.abs(matches['Home Team Goals'] - matches['Away Team Goals'])

top10_difference = matches.sort_values(by='difference_goals', ascending=False)[:10]

top10_difference['difference_goals'] = top10_difference['difference_goals'].astype(int)

top10_difference['difference_goals_str'] = top10_difference['difference_goals'].astype(str) + " goals difference"

top10_difference['result'] = top10_difference['Home Team Goals'].astype(str) + "-" + top10_difference[
    'Away Team Goals'].astype(str)

plt.figure(figsize=(12, 10))
ax = sns.barplot(y=top10_difference['VS'], x=top10_difference['difference_goals'])
sns.despine(right=True)
plt.ylabel('Teams')
plt.xlabel('Goals')
plt.yticks(size=10)
plt.xticks(size=10)
plt.title('Top10 Biggest Difference Matches', size=20)

for i, s in enumerate(
        "Stadium " + top10_difference['Stadium'] + ", Date: " + top10_difference['Datetime'] + "\n" + "stage: " +
        top10_difference['Stage'] + ".  " +
        top10_difference['difference_goals_str'] + ", match result: " + top10_difference['result']):
    ax.text(1, i, s, fontsize=12, color='white', va='center')

# #######################################################################################################################################################
matches = matches.replace(['Germany FR'], 'Germany')

list_countries = matches['Home Team Name'].unique().tolist()

# 分主客队来统计：
lista_home = []
lista_away = []
for i in list_countries:
    goals_home = matches.loc[matches['Home Team Name'] == i, 'Home Team Goals'].sum()
    lista_home.append(goals_home)
    goals_away = matches.loc[matches['Away Team Name'] == i, 'Away Team Goals'].sum()
    lista_away.append(goals_away)

df = pd.DataFrame({'country': list_countries, 'total_home_goals': lista_home, 'total_away_goals': lista_away})
df['total_goals'] = df['total_home_goals'] + df['total_away_goals']
most_goals = df.sort_values(by='total_goals', ascending=False)[:10]
print(most_goals)

####################################################################################################################
fig, ax = plt.subplots(figsize=(16, 8))

plt.title('Top Goals Number by Country', size=16, weight='bold')
most_goals.plot(x="country", y=["total_home_goals", "total_away_goals", "total_goals"], kind="bar", ax=ax)

ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.set_ylabel(None)
ax.set_xlabel(None)
ax.tick_params(labelleft=False, left=False, labelsize=8)
ax.legend(fontsize=10)

for i in ax.containers:
    ax.bar_label(i, fontsize=10)
# #################################################################################################################################
# finalist来自“世界杯成绩分析表”小节，表示进入决赛的队伍
finalista = WorldCupsSummary.finalist['index'].tolist()

# 分主、客场统计：
goalsconceded_home = []
goalsconceded_away = []
match1 = []
match2 = []
for i in finalista:
    goalsconc_home = matches.loc[matches['Home Team Name'] == i, 'Away Team Goals'].sum()
    goalsconceded_home.append(goalsconc_home)
    goalsconc_away = matches.loc[matches['Away Team Name'] == i, 'Home Team Goals'].sum()
    goalsconceded_away.append(goalsconc_away)
    counted1 = (matches['Home Team Name'] == i).sum()
    counted2 = (matches['Away Team Name'] == i).sum()

    match1.append(int(counted1))
    match2.append(int(counted2))

# 按照失球数排序

df = pd.DataFrame(
    {'country': finalista, 'goalsconceded_home': goalsconceded_home, 'goalsconceded_away': goalsconceded_away,
     'matches_home': match1, 'matches_away': match2})
df['total_matches'] = df['matches_home'] + df['matches_away']
df['total_goalsconceded'] = df['goalsconceded_home'] + df['goalsconceded_away']
df['goalmatch_rate'] = (df['total_goalsconceded'] / df['total_matches']).round(2)
goals_conceded = df.sort_values(by='goalmatch_rate')[:10]
print(goals_conceded)
fig, ax= plt.subplots(nrows=1,ncols=2,figsize=(20,8))

goals_conceded.plot(x="country", y="total_goalsconceded", kind="bar",ax=ax[0])

ax[0].set_title('Total Goals Conceded by Country',size=20,weight='bold')
ax[0].spines['right'].set_visible(False)
ax[0].spines['top'].set_visible(False)
ax[0].spines['left'].set_visible(False)
ax[0].spines['bottom'].set_visible(False)
ax[0].set_ylabel(None)
ax[0].set_xlabel(None)
ax[0].tick_params(labelleft=False, left=False,labelsize=14)

for i in ax[0].containers:
    ax[0].bar_label(i,fontsize=15)

goals_conceded.plot(x="country", y="goalmatch_rate", kind="bar",ax=ax[1])

ax[1].set_title('Goals Conceded Ratio by Country',size=20,weight='bold')
ax[1].spines[['right', 'top', 'left']].set_visible(False)
ax[1].set_ylabel(None)
ax[1].set_xlabel(None)
ax[1].tick_params(labelleft=False, left=False,labelsize=14)

for i in ax[1].containers:
    ax[1].bar_label(i,fontsize=15)
plt.show()
wrds = matches["Home Team Name"].value_counts().keys()
wc = wordcloud.WordCloud(scale=5,max_words=1000,colormap="rainbow").generate(" ".join(wrds))
plt.figure(figsize=(13,14))
plt.imshow(wc,interpolation="bilinear")
plt.axis("off")
plt.title("Home Team Name - word cloud",color='b')

wrds = matches["Away Team Name"].value_counts().keys()
wc = wordcloud.WordCloud(scale=5,max_words=1000,colormap="rainbow").generate(" ".join(wrds))
plt.figure(figsize=(13,14))
plt.imshow(wc,interpolation="bilinear")
plt.axis("off")
plt.title("Away Team Name - word cloud",color='b')
plt.show()